﻿#region itdc

/************************************************************************
*Copyright(C) 2015 by qsgy
 *All rights reserved. 
 *FileName:     $safeitemname$
 *Author:       oyh
 *Version:      
 *UnityVersion：
 *Date:   $time$      
 *Description:    
 *History: 
************************************************************************/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public class TextClass:Singleton<TextClass>
{
#region 功能函数
    /// <summary>
    /// 创建保存关卡信息或者音乐信息的ArrayList
    /// </summary>
    /// <param name="isMusicInform"></param>
    /// <returns></returns>
    public ArrayList Creat(bool isMusicInform)
    {
        bool isOpen = true;
        ArrayList arrayList = new ArrayList();
        if (!isMusicInform)
        {
            for (int i = 1; i <= 24; i++)
            {
                if (i != 1) isOpen = false;
                SaveScores sScore = new SaveScores(i, 0, isOpen);
                arrayList.Add(sScore);
            }
        }
        else
        {
            MusicInform musicInform1 = new MusicInform(MusicOrSound.Music,true,1);
            MusicInform musicInform2 = new MusicInform(MusicOrSound.Sound, true, 1);
            arrayList.Add(musicInform1);
            arrayList.Add(musicInform2);
        }
        return arrayList;
    }

    /// <summary>
    /// 存储相关信息的ArrayList
    /// </summary>
    /// <param name="ayList"></param>
    public bool Save(ArrayList ayList ,string path) //序列化保存
    {
        try
        {
            FileStream fs = new FileStream(Application.persistentDataPath + @"/"+path, FileMode.Create,FileAccess.Write);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, ayList);
            fs.Close();
            return true;
        }
        catch (Exception e)
        { print(e);return false; }
    }

    /// <summary>
    /// get是为真的时候是否直接返回数不做处理
    /// isNewOpen为真的时候说明新开启一个关卡
    /// cp是指关卡序号
    /// score是当前关卡分数，默认为0
    /// </summary>
    /// <param name="get"></param>
    /// <param name="isNewOpen"></param>
    /// <param name="cp"></param>
    /// <param name="score"></param>
    /// <returns></returns>
    public ArrayList Load(bool get,bool isNewOpen=false,int cp=0,float score=0)
    {
        ArrayList aList=new ArrayList();
        try
        {
            FileStream fs = new FileStream(Application.persistentDataPath+ @"/CpScore.yzc", FileMode.Open, FileAccess.ReadWrite);
            BinaryFormatter bf = new BinaryFormatter();
            aList = (ArrayList)bf.Deserialize(fs);
            fs.Close();

        }
        catch (Exception e){ print(e); }
        if (get) return aList;
        return Modify(aList, isNewOpen, score, cp);
    }//反序列化取出

    /// <summary>
    /// get是为真的时候是否直接返回数不做处理
    /// saveSuccess 是否保存成功
    /// isMusicopen，isSoundOpne是否开起音乐，音效声音
    /// mVolume,sVolume音量大小
    /// </summary>
    /// <param name="get"></param>
    /// <param name="saveSuccess"></param>
    /// <param name="isMusicOpen"></param>
    /// <param name="isSoundOpen"></param>
    /// <param name="mVolume"></param>
    /// <param name="sVolume"></param>
    /// <returns></returns>
    public ArrayList MFLoad(bool get,out bool saveSuccess, bool isMusicOpen=false,bool isSoundOpen=false,float mVolume=0,float sVolume=0)
    {
        
        ArrayList aList = new ArrayList();
        try
        {
            FileStream fs = new FileStream(Application.persistentDataPath + @"/MIF.yzc", FileMode.Open, FileAccess.ReadWrite);
            BinaryFormatter bf = new BinaryFormatter();
            aList = (ArrayList)bf.Deserialize(fs);
            fs.Close();
        }
        catch (Exception e) { print(e); }
        if (get) { saveSuccess = false; return aList; }
        saveSuccess= Save(Modify(aList,isMusicOpen, isSoundOpen, mVolume, sVolume), "MIF.yzc");
        return null;
    }

    /// <summary>
    /// isOpen为true时修改分数而是新打开一个关卡
    /// scroe是分数
    /// index关卡序号
    /// </summary>
    /// <param name="arrayList"></param>
    /// <param name="isOpen"></param>
    /// <param name="score"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public ArrayList Modify(ArrayList arrayList,bool isOpen,float score,int index)
    {
        SaveScores ss =(SaveScores) arrayList[index-1];
        if (ss.Score < score)
        {
            arrayList.Remove(ss);
            ss.Score = score;
            arrayList.Insert(index - 1, ss);
        }
        if(isOpen==true)
        {
            SaveScores ss1 = (SaveScores)arrayList[index];
            if (ss1.IsOPen != true)
            {
                arrayList.Remove(ss1);
                ss1.IsOPen = isOpen;
                arrayList.Insert(index, ss1);
            }
        }
        return arrayList;
    }//修改信息

    /// <summary>
    /// arratlist是需要修改的数组
    /// isMusicopen，isSoundOpne是否开起音乐，音效声音
    /// mVolume,sVolume音量大小
    /// </summary>
    /// <param name="arrayList"></param>
    /// <param name="isMusicOpen"></param>
    /// <param name="isSoundOpen"></param>
    /// <param name="mVolume"></param>
    /// <param name="sVolume"></param>
    /// <returns></returns>
    public ArrayList Modify(ArrayList arrayList, bool isMusicOpen, bool isSoundOpen, float mVolume, float sVolume)
    {
        int i = 0;
        foreach (MusicInform mif in arrayList)
        {
            if (i == 0)
            {
                mif.IsOpen = isMusicOpen;
                mif.Volume = mVolume;
            }
            else
            {
                mif.IsOpen = isSoundOpen;
                mif.Volume = sVolume;
            }
            i++;
        }
        return arrayList;
    }

    public override void FixedAwake()
    {
    }
#endregion
}
/// <summary>
/// 用于保存关卡信息和分数的类
/// </summary>
[Serializable]
public class SaveScores
{
    
    private int checkPoint;//当前关卡序号
    private float score;//获取的分数
    private bool isOpen;//是否已经开启
    # region 属性
    public bool IsOPen
    {
        get { return isOpen; }
        set { isOpen = value; }
    }
    public int CheckPoint
    {
        get { return checkPoint; }
        set { checkPoint = value; }
    }
    public float Score
    {
        get { return score; }
        set { score = value; }
    }
    #endregion
    #region 构造函数
    public SaveScores(int cp,float scr,bool isopen)
    {
        checkPoint = cp;
        score = scr;
        isOpen = isopen;
    }
    public SaveScores() { }
#endregion
}
/// <summary>
/// 用来记录声音的大小
/// </summary>
[Serializable]
public class MusicInform
{
    private MusicOrSound musicOr;//声音的种类
    private bool isOpen;//是否播放
    private float volume;//声音的大小
#region 属性
    public MusicOrSound MusicOr
    {
        get {return musicOr; }
    }
    public bool IsOpen
    {
        get { return isOpen; }
        set { isOpen = value; }
    }
    public float Volume
    {
        get { return volume; }
        set { volume = value; }
    }
    #endregion
#region 构造函数
    public MusicInform(MusicOrSound c, bool a,float b)
    {
        musicOr = c;
        isOpen = a;
        volume = b;
    }
#endregion
}

